<?php
namespace app\farm\model;

use app\BaseModel;
use app\member\model\Level;
use think\facade\Db;

class Coupon extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_coupon';


    protected $append = [


        'send_count',


    ];


    /**
     * @author chenniang
     * @DataTime: 2021-07-15 15:22
     * @功能说明:已派发多少张
     */
    public function getSendCountAttr($value,$data){

        if(!empty($data['id'])){

            $record_model = new CouponRecord();

            $count = $record_model->where(['coupon_id'=>$data['id']])->sum('num');

            return $count;

        }

    }







    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

       // $data_data = $data;

//        if(isset($data['service'])){
//
//            unset($data['service']);
//        }
//
//        if(isset($data['member_level'])){
//
//            unset($data['member_level']);
//        }
//
//        if(isset($data['shop_goods'])){
//
//            unset($data['shop_goods']);
//        }



        $res = $this->insert($data);

        $id  = $this->getLastInsID();

       // $this->updateSome($id,$data_data);

        return $id;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

       // $data_data = $data;

//        if(isset($data['service'])){
//
//            unset($data['service']);
//        }
//
//        if(isset($data['member_level'])){
//
//            unset($data['member_level']);
//        }
//
//        if(isset($data['shop_goods'])){
//
//            unset($data['shop_goods']);
//        }
//
//        if(isset($data['restaurant_goods'])){
//
//            unset($data['restaurant_goods']);
//        }

        $res = $this->where($dis)->update($data);

      //  $this->updateSome($dis['id'],$data_data);

        return $res;

    }


    /**
     * @param $id
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2021-11-01 15:40
     */
    public function updateSome($id,$data){

        $server = new \app\shop\server\Coupon();

        $s_model = new CouponService();

        $coupon_member_model = new CouponMember();

        $goods_model = new \app\shop\model\Goods();

        $r_goods_model = new \app\restaurant\model\Goods();

        $coupon_member_model->where(['coupon_id'=>$id])->delete();

        $s_model->where(['coupon_id'=>$id])->delete();
        //赛车服务
        $server->addObserver($s_model);
        //会员
        $server->addObserver($coupon_member_model);
        //商城商品
        $server->addObserver($goods_model);
        //
        $server->addObserver($r_goods_model);

        $res = $server->notify($id,$data);

        return $res;

    }



    /**
     * @param $id
     * @param $uniacid
     * @param $spe
     * @功能说明:
     * @author chenniang
     * @DataTime: 2021-03-23 13:35
     */
    public function updateSomev2($id,$uniacid,$goods,$member_level,$shop_goods){

        $s_model = new CouponService();

        $coupon_member_model = new CouponMember();

        $s_model->where(['coupon_id'=>$id])->delete();

        if(!empty($goods)){

            foreach ($goods as $value){

                $insert['uniacid']   = $uniacid;

                $insert['coupon_id'] = $id;

                $insert['goods_id']  = $value;

                $s_model->dataAdd($insert);

            }

        }

        if(!empty($shop_goods)){

            foreach ($shop_goods as $value){

                $insert['uniacid']   = $uniacid;

                $insert['coupon_id'] = $id;

                $insert['goods_id']  = $value;

                $insert['scene']     = 2;

                $s_model->dataAdd($insert);

            }

        }

        $coupon_member_model->where(['coupon_id'=>$id])->delete();

        if(!empty($member_level)){

            foreach ($member_level as &$value){

                $inserts['uniacid']       = $uniacid;

                $inserts['coupon_id']     = $id;

                $inserts['member_level']  = $value;

                $coupon_member_model->dataAdd($inserts);

            }

        }

        return true;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis,$filed='*'){

        $data = $this->where($dis)->field($filed)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-09 23:22
     * @功能说明:计算优惠券可以优惠多少钱
     */

    public function getDicountPrice($order_goods,$coupon_id=0){

//        $coupon_se_model = new CouponService();
            //暂时没有商品限制 先注释
//        $goods_id = $coupon_se_model->where(['coupon_id'=>$coupon_id,'type'=>1,'scene'=>$scene])->column('goods_id');

        $price = 0;

        foreach ($order_goods as $v){

            foreach ($v['goods_list'] as $vs){

//                if(in_array($vs['goods_list'],$goods_id)){

                    $price += $v['car_price'];
                    //暂时没有商品限制
                     $goods_id[] = $vs['goods_id'];
//                }

            }
        }

        $data['discount']  = $price;

        $data['goods_id']  = $goods_id;

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-09 23:37
     * @功能说明:订单优惠券
     */
    public function orderCouponData($order_goods,$coupon_id,$scene=1){

        if(empty($coupon_id)){

            return $order_goods;
        }

        $coupon_record_model = new CouponRecord();

        $info = $coupon_record_model->dataInfo(['id'=>$coupon_id]);
        //是否被使用或者过期
        if(empty($info)||$info['status']!=1){

            return $order_goods;
        }
        //不支持商城
        if($scene==1&&$info['is_shop']!=1){

            return $order_goods;

        }
        //不支持土地
        if($scene==2&&$info['is_land']!=1){

            return $order_goods;

        }
        //不支持认养
        if($scene==3&&$info['is_claim']!=1){

            return $order_goods;

        }
        if($info['start_time']<time()&&$info['end_time']>time()){
            //是否满足满减条件
            if($info['full']>$order_goods['car_price']){

                return $order_goods;
            }

            $total_discount = 0;

            foreach ($order_goods['list'] as $ks=> $vs){

                $ks_discount = 0;

                foreach ($vs['goods_list'] as &$v){

                    $bin = $v['true_price']/$order_goods['car_price'];
                    //该商品减去的折扣
                    $discount = $bin*$info['discount']<$v['true_price']?$bin*$info['discount']:$v['true_price'];
                    //总计折扣
                    $total_discount+=$discount;

                    $ks_discount   +=$discount;

                    $v['true_price'] = round($v['true_price']-$discount,2);

                }

                $order_goods['list'][$ks]['coupon_discount'] = $ks_discount;

            }

            $total_discount = $info['full']>$info['discount']?$info['discount']:round($total_discount,2);

            $order_goods['total_discount'] = round($total_discount,2);

            $order_goods['coupon_id'] = $coupon_id;

        }

        return $order_goods;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-13 11:58
     * @功能说明:用户可用的优惠券
     */
    public function canUseCoupon($user_id,$order_price,$type=1){

        $coupon_model = new CouponRecord();

        $dis = [

            'user_id' => $user_id,

            'status'  => 1
        ];

        switch ($type){

            case 1:
                //商城支持
                $dis_type = 'is_shop';
                break;
            case 2:
                //土地支持
                $dis_type = 'is_land';

                break;
            case 3:
                //认养支持
                $dis_type = 'is_claim';

                break;


        }

        $coupon_model->where($dis)->where('end_time','<',time())->update(['status'=>3]);

        $dis[$dis_type] = 1;

        $list = $coupon_model->where($dis)->where('full','<=',$order_price)->column('id');

        return $list;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-28 10:50
     * @功能说明:商城用户可以领取的优惠券
     */
    public function shopCanGetCoupon($uniacid,$user_id,$page=10){

        $record_model = new CouponRecord();

        $id = $record_model->where(['user_id'=>$user_id])->column('coupon_id');

        $dis = [

            'uniacid' => $uniacid,

            'status'  => 1,

            'send_type'=> 2

        ];

        $where[] = ['time_limit','=',1];

        $where[] = ['end_time','>',time()];


        $data = $this->where($dis)->where('id','not in',$id)->where(function ($query) use ($where){
            $query->whereOr($where);
        })->order('top desc,id desc')->paginate($page)->toArray();

        return $data;
    }







}